#ifndef DB_H
#define DB_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "setup.h"
#include "sqlite3.h"
#ifdef __cplusplus
extern "C" {
#endif
#define MAX_RED 33
#define MAX_BLUE 16
#define TERMLENGTH 8
#define BLANK "          " //10 space
struct termrecord
 {
 char desc[256];	
 char term[TERMLENGTH];
 char red1;	
 char red2;
 char red3;
 char red4;
 char red5;
 char red6;
 char blue;
 };
 
 struct statics
 {
 	struct termrecord record;
 	struct termrecord nextrecord;//for real next
    struct termrecord predrecord;//for predication
    char distance[32];//distance statics counter
 	unsigned int count;
 	};
enum DB_BOOL {  NOT_MATCH=0,  MATCH=1 };
typedef struct statics  STATICS;
 
typedef struct termrecord  RECORD;
typedef enum DB_BOOL DB_BOOL;
typedef  RECORD (*funcptr)(RECORD data)  ;//  declare function point strict with RECORD argument


     static char  _LastTermOrder[20];//最后一期数据
     static char  _LastTermDate[20];//最后一期日期
     static char  _FirstTermOrder[20];//第一期数据
     static char  _FirstTermDate[20];//第一期数据


     static char  DB_LOCATION_HOME[1024]="doubleball.db3";
//     char **CURRENT_RECORDS;//所有记录
//     int CURRENT_ROWS;
//     int CURRENT_COLS;
//     sqlite3 *CURRENT_DB;

    static char QUERY_DB[4096]="SELECT trim(term) as term,red1,red2,red3,red4,red5,red6,blue FROM _DBHISTORY order by term asc";;

int query(sqlite3 *CURRENT_DB,char * sql,char ***CURRENT_RECORDS,int* CURRENT_ROWS,int* CURRENT_COLS);

RECORD* rowsToRecords(char**CURRENT_RECORDS,int CURRENT_ROWS,int CURRENT_COLS);
RECORD getDBFormer(RECORD data);//get previous record
char * formatRecord(RECORD indata);
void  printRecord(RECORD indata);
void printSTATICS(STATICS* indata,int size);
void setPatternDesc(RECORD* rec,char *str);
void setTerm(RECORD* rec,char * str);
char BUFF[256];
void Debug(char * msg);
int comp (const void * elem1, const void * elem2) ;//use for qsort
char * sortBall(char * data,int length);
int getNextTerm(char *term);
void copyRECORD(RECORD *src,RECORD *tgt);
#ifdef __cplusplus
}  /* end of the 'extern "C"' block */
#endif
#endif
